package com.touch.jgloo.owt;
import java.awt.event.*;

import com.touch.jgloo.core.*;
/**
 * JGLTextBox is a JGLComponent that allows input in the form of text
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r419
 * @see com/touch/Licence
 */
public final class JGLTextBox extends JGLComponent implements KeyListener
{
	//background object
	private final JGLQuad background;
	//foreground object
	private final JGLQuad foreground;
	//cursor object
	private final JGLQuad selected;
	//caption object
	private final JGLTextObject text;
	//cursor position
	private int cursor = 0;
	/**
	 * Create a JGLTextBox and define its width and length of text
	 * @param width width of the JGLTextBox
	 * @param bufferSize length of the JGLTextBox's String
	 * @since r461
	 */
	public JGLTextBox(final int width, final int bufferSize)
	{
		//create the background object
		background = new JGLQuad(width, 16);
		//create the foreground object
		foreground = new JGLQuad(width - 4, 12);
		//create the cursor object
		selected = new JGLQuad(8, 16);
		
		//set the size of the JGLTextBox
		this.setSize(width, 16);		
		//create a char array of blank chars
		char[] buffer = new char[bufferSize];
		for(int i = 0; i < buffer.length; i++)
			buffer[i] = ' ';
		//create a String from the char array
		String def = new String(buffer);
		//create the caption object from the String of blank chars		
		text = JGLTextObject.createTextObject(new String(def), JGLComponent.TEXT_TYPE);			
			
		//apply the default for component style sheet
		//this.applyStyleSheet(JGLStyleSheet.DEFAULT_COMPONENT);
	}
	/**
	 * Create a JGLTextBox and define the width and default text
	 * @param width width of the JGLTextBox
	 * @param defaultText default text of the JGLTextBox
	 * @since r461
	 */
	public JGLTextBox(final int width, final String defaultText)
	{		
		//create the background object
		background = new JGLQuad(width, 16);
		//create the foreground object
		foreground = new JGLQuad(width - 4, 12);
		//create the cursor object
		selected = new JGLQuad(8, 16);
		//set the size of the object
		this.setSize(width, 16);
					
		//create the caption object
		text = JGLTextObject.createTextObject(defaultText, JGLComponent.TEXT_TYPE);
		
		//apply the stylesheet. use the Component default
		this.applyStyleSheet(JGLStyleSheet.DEFAULT_INPUT);		
	}	
	public final  void resized(final float w, final float h)
	{
		//resize the foreground
		foreground.resize(w - 4, h - 4);
		//resize the background
		background.resize(w, h);		
	}	
	public final  void repositioned(final float x, final float y, final float z) {}
	public final  void render()
	{	
		if(this.isVisible() == false)
			return;
		GL_INSTANCE.glPushMatrix();			
			GL_INSTANCE.glTranslatef(
				this.getPosition().x, this.getPosition().y, this.getPosition().z);				
			background.render();		
			GL_INSTANCE.glPushMatrix();
				GL_INSTANCE.glTranslatef(2, 2, 0);
				foreground.render();
			GL_INSTANCE.glPopMatrix();			
			text.render();
			if(getSelected() == this.getIndex())
			{ 
				GL_INSTANCE.glTranslatef(6 + 10 * cursor, 0, 0);			
				selected.render();
			}
		GL_INSTANCE.glPopMatrix();		
	}
	/**
	 * Sets the text of the caption
	 * @param text
	 */
	public final  void setText(final String text)
	{
		//set the caption's text
		this.text.setCaption(text);
	}
	/**
	 * Returns the text of the caption
	 * @return Caption's text as a String
	 */
	public final  String getText()
	{
		//sets the caption's text
		return this.text.getCaption();
	}
	public final void keyPressed(final KeyEvent e) 
	{
		if(this.isVisible() == false)
			return;
		if(getSelected() != this.getIndex())
			return;		
		if(e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			if(cursor > 0)
				cursor --;
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			if(cursor < text.length() - 1)		
				cursor ++;
		}	 				
	}
	public final void keyReleased(final KeyEvent e)	{}
	public final void keyTyped(final KeyEvent e) 
	{		
		if(this.isVisible() == false)
			return;
		if(getSelected() != this.getIndex())
			return;
		if(e.getKeyChar() == 8)
		{
			if(cursor >= 0)
			{
				text.setCaption(cursor, ' ');
				
				if(cursor > 0)
					cursor --;
			}			
		}
		else if(e.getKeyChar() == 127)
		{
			text.setCaption(cursor, ' ');
		}
		else if(cursor < text.length() - 1)
		{				
			text.setCaption(cursor, e.getKeyChar());		
			cursor++;
		}
			
	}
	public final  void clean() 
	{	
		background.clean();
		foreground.clean();
		selected.clean();
	}
	public final  void compile()
	{
		background.compile();
		foreground.compile();
		selected.compile();
	}
	/**
	 * Applies the JGLStyleSheet to the JGLTextbox
	 * the cursor uses the componentForegroundTexture and the componentForegroundColor
	 * the text uses the componentBackgroundTexture and the componentBackgroundColor
	 */	
	public final  void applyStyleSheet(final JGLStyleSheet ss) 
	{	
		background.setTexture(ss.backgroundTexture);
		background.setColor(ss.backgroundColor);
		background.setUV(JGLUVType.QUADMAP);
		foreground.setTexture(ss.foregroundTexture);
		foreground.setColor(ss.foregroundColor);
		foreground.setUV(JGLUVType.QUADMAP);
		text.setTexture(ss.componentBackgroundTexture);
		text.setColor(ss.componentBackgroundColor);
		selected.setTexture(ss.componentForegroundTexture);
		selected.setColor(ss.componentForegroundColor);
		selected.setUV(JGLUVType.QUADMAP);
	}			
}
